package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.order.CancelOrderService;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(check = false)
    CancelOrderService cancelOrderService;

    @Reference(check = false, retries = 0)
    OrderCoreService orderCoreService;

    @Reference(check = false, retries = 0)
    OrderQueryService orderQueryService;

    //创建订单
    @PostMapping("order")
    @ApiOperation("创建订单")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        String userInfo=(String)servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object= JSON.parseObject(userInfo);
        Long uid=Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        //设置uniquekey
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response=orderCoreService.createOrder(request);
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 取消订单
     * @return
     */
    @PostMapping("/cancelOrder")
    @Anoymous
    public ResponseData cancelOrder(@RequestBody Map map){
        String id = (String) map.get("orderId");
        CancelOrderRequest request = new CancelOrderRequest();
        String orderId = String.valueOf(id);
        request.setOrderId(orderId);
        CancelOrderResponse response = cancelOrderService.cancelOrder(request);
        if (!response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil().setData(response.getMsg());
    }

    //查询订单详情
    @GetMapping("order/{id}")
    public ResponseData GetOrderDetail(@PathVariable("id") String orderId){
        OneOrderDetailRequest request = new OneOrderDetailRequest();
        request.setOrderId(orderId);
        OneOrderDetailResponse response = orderQueryService.getOneOrderDetail(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<OneOrderDetailResponse>().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //删除订单
    @RequestMapping(value = "order/{id}", method = RequestMethod.DELETE)
    public ResponseData deleteOrder(@PathVariable("id") String id, HttpServletRequest request) {
        if (id == null) {
            return new ResponseUtil<String>().setErrorMsg(Integer.parseInt(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode()), OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        String userInfo = (String) request.getAttribute("userInfo");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String username = jsonObject.get("username").toString();
        Integer result = orderCoreService.deleteOrderByOrderId(id,username);
        return result != 1 ? new ResponseUtil<String>().setErrorMsg(Integer.parseInt(OrderRetCode.PIPELINE_RUN_EXCEPTION.getCode()),
                OrderRetCode.PIPELINE_RUN_EXCEPTION.getMessage()):
                new ResponseUtil<String>().setData(OrderRetCode.SUCCESS.getMessage());
    }

    /**
     * 获取用户所有订单
     * @param request
     * @param servletRequest
     * @return
     */
    @GetMapping("order")
    public ResponseData getUserAllOrder(AllUserOrderRequest request, HttpServletRequest servletRequest) {
        //从token里面获得用户id
        String userInfo = (String) servletRequest.getAttribute("userInfo");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        String username = jsonObject.get("username").toString();
        request.setUsername(username);
        AllUserOrderResponse userAllOrder = orderQueryService.getUserAllOrder(request);
        if (!userAllOrder.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(userAllOrder.getMsg());
        }
        return new ResponseUtil().setData(userAllOrder);
    }
}
